// pages/my/address-detail/address-detail.js
var util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    telephone: "",
    address_id: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      address_id: options.id
    })
    wx.setNavigationBarTitle({
      title: '修改地址',
    })

    // 获取本地数据-地址列表
    wx.getStorage({
      key: 'address_list',
      // 能获取到则显示用户信息，保存信息到页面
      success: (json) => {
        console.log(json.data.address_list);
        this.setData({
          address_list: json.data.address_list
        })
        let _address_list = that.data.address_list; // 拿到标记数组
        let addressId = options.id;
        let addressIndexInfo;
        _address_list.map(function (m) {
          if (m.id == addressId) {
            addressIndexInfo = m;
          }
        });
        console.log(addressIndexInfo);
        that.setData({
          addressIndexInfo: addressIndexInfo
        })
      }
    });
    // 获取用户信息
    wx.getStorage({
      key: 'userInfo',
      success: function (json) {
        that.setData({
          userInfo: json.data.userInfo
        })
        console.log(that.data.userInfo);
      },
    })
  },

  phoneinput(e) {
    console.log(e)
    let value = e.detail.value
    console.log(value)
    this.setData({
      telephone: value
    })
  },

  changeAddress: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var that = this;
    var formData = e.detail.value;
    var uid = that.data.userInfo.uid;
    var userInfo = that.data.userInfo;
    console.log(userInfo)
    if (formData.name == '') {
      wx.showToast({
        title: "请输入联系人",
        icon: "loading"
      })
    } else if (formData.mobile == '' || formData.mobile.length != 11 || isNaN(formData.mobile)) {
      wx.showToast({
        title: "手机号不正确",
        icon: "loading"
      })
    } else if (formData.address == '') {
      wx.showToast({
        title: "请输入详细地址",
        icon: "loading"
      })
    } else if (uid && formData.mobile && formData.name && formData.address) {
      wx.request({
        url: 'https://boxapi.supergk.com/server/user/update_user_address',
        // data: formData,
        data: util.json2Form({ id: this.data.address_id, user_id: uid, tel: formData.mobile, name: formData.name, address: formData.address }),
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        method: "POST",
        success: function (res) {
          console.log(res.data)
          if (res.data.code == "0") {//修改地址成功
            console.log(res.data.msg);
            wx.showToast({
              title: '修改成功',
              icon: 'success'
            })
            setTimeout((function callback() {
              wx.navigateTo({
                url: '../address/address',
              })
            }).bind(this), 1500);
          } else {
            console.log(res.data.msg);
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }

        },
        fail: function (res) {

        },
        complete: function (res) { },
      })
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})